package com.chuanyang.driverh.mainhome.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.mainhome.adapter.DriverJoinApplyAdapter;
import com.chuanyang.driverh.response2.join.ResultDriverApplyList;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.LogUtils;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class DriverJoinApplyFragment extends Fragment {

    private ListView lv_jiameng_apply;
    private DriverJoinApplyAdapter driverJoinApplyAdapter;

    private List<ResultDriverApplyList.DataBean> driverApplyData = new ArrayList<>();

    private String fleet = "";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_driver_join_apply, container, false);

        driverJoinApplyAdapter = new DriverJoinApplyAdapter(getActivity(),mListener,mListener1);
        //driverJoinApplyAdapter.setNewData(driverApplyData);

        lv_jiameng_apply = (ListView) view.findViewById(R.id.lv_jiameng_apply);
        lv_jiameng_apply.setAdapter(driverJoinApplyAdapter);

        if(getActivity().getIntent().getStringExtra("fleet") != null){
            fleet = getActivity().getIntent().getStringExtra("fleet");
            if(fleet.equals("1")){//车队
                selectApplyInfo("","2",SharedPrefUtil.getFleetId());
            }else if(fleet.equals("2")){//司机
                selectApplyInfo(SharedPrefUtil.getUserId(),"1","");
            }
        }

        return view;
    }

    private DriverJoinApplyAdapter.HuLueClickListener mListener = new DriverJoinApplyAdapter.HuLueClickListener() {
        @Override
        public void mListener(int position, View v) {//忽略的点击事件
            //Toast.makeText(getActivity(),driverApplyData.size()+"s"+position,Toast.LENGTH_SHORT).show();
            refuseBusinessRelationship(driverApplyData.get(position).getBusinessId()+"");
        }
    };
    private DriverJoinApplyAdapter.JieShouClickListener mListener1 = new DriverJoinApplyAdapter.JieShouClickListener() {
        @Override
        public void mListener1(int position, View v) {//接受的点击事件
            //Toast.makeText(getActivity(),driverApplyData.size()+"s2"+position,Toast.LENGTH_SHORT).show();
            acceptBusinessRelation(driverApplyData.get(position).getBusinessId()+"");
        }
    };
    /**
     * 查看申请栏信息接口
     * @param id
     */
    private void selectApplyInfo(String id,String type,String fleetId){
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,Object> paramssss =new HashMap<String, Object>();
        paramssss.put("type", type);//1：司机 2：车队长
        paramssss.put("driverId", id);
        paramssss.put("showType", "1");
        paramssss.put("fleetId", fleetId);
        MyHttpUtil.selectApplyInfo(getActivity(),paramssss, new MyCallback<ResultDriverApplyList>(
                getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {

            }
            @Override
            public void onSuccess(ResultDriverApplyList result, int id) {

                if (result.getCode() == ConstUtil.OK ) {
                    try {
                        driverApplyData.clear();
                        driverApplyData.addAll(result.getData());
                        driverJoinApplyAdapter.setNewData(driverApplyData);

                    } catch (Exception e) {
                    }
                }else{
                    if (!StringUtil.isBlank(result.getMsg()))
                        PrintUtil.toast(getActivity(),result.getMsg());
                }


            }
        });
    }


    /**
     * 忽略加盟的接口
     */
    private void refuseBusinessRelationship(String id){
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,Object> paramssss =new HashMap<String, Object>();
        paramssss.put("id", id);//1：司机 2：车队长
        paramssss.put("apply", "2");

        MyHttpUtil.refuseBusinessRelationship(getActivity(),paramssss, new MyCallback<Result>(
                getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {

            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK ) {
                    try {
                        if(getActivity().getIntent().getStringExtra("fleet") != null){
                            fleet = getActivity().getIntent().getStringExtra("fleet");
                            if(fleet.equals("1")){//车队
                                selectApplyInfo("","2",SharedPrefUtil.getFleetId());
                            }else if(fleet.equals("2")){//司机
                                selectApplyInfo(SharedPrefUtil.getUserId(),"1","");
                            }
                        }
                    } catch (Exception e) {
                    }
                }else{
                    if (!StringUtil.isBlank(result.getMsg()))
                        PrintUtil.toast(getActivity(),result.getMsg());
                }

            }
        });
    }

    /**
     * 接受加盟
     * @param id
     */
    private void acceptBusinessRelation(String id){
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,Object> paramssss =new HashMap<String, Object>();
        paramssss.put("id", id);
        paramssss.put("apply", "1");

        MyHttpUtil.acceptBusinessRelation(getActivity(),paramssss, new MyCallback<Result>(
                getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {

            }
            @Override
            public void onSuccess(Result result, int id) {
                LogUtils.d(result.getCode()+result.getMsg());
                if(getActivity().getIntent().getStringExtra("fleet") != null){
                    fleet = getActivity().getIntent().getStringExtra("fleet");
                    if(fleet.equals("1")){//车队
                        selectApplyInfo("","2",SharedPrefUtil.getFleetId());
                    }else if(fleet.equals("2")){//司机
                        selectApplyInfo(SharedPrefUtil.getUserId(),"1","");
                    }
                }
            }
        });
    }

}
